<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2015/9/5
 * Time: 14:34
 */

class SiteModule extends Data{
    /** @return SiteModule */
    public static function init() {
        return parent::init();
    }

    function getCookieSiteId(){
        $siteId=isset($_COOKIE['site_id'])? (int)$_COOKIE['site_id']: 1;
        $siteId=empty($siteId)? 1: $siteId;
        return $siteId;
    }

    function getList($rows = 30,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `site` LIMIT $offset,$rows";
        return $this->db->getList($sql);
    }

    function getDetail($site_id){
        $sql = "SELECT * FROM `site` WHERE `id`=?";
        return $this->db->getOne($sql,$site_id);
    }

    function add($name,$map_address='',$longitude='',$latitude='',$username,$email,$password){
        $data['name']=$name;
        $data['map_address']=$map_address;
        $data['lon']=$longitude;
        $data['lat']=$latitude;
        $user['site_id']=$this->db->insert("site",$data)==1 ? (int)$this->db->insertId(): -1;
        if($user['site_id']){
            $user['username']=$username;
            $user['email']=$email;
            $user['code']=getPassWord($username,$password);
            $user['levels']="2,3,4,5,6,7,8,9,10,11,12,13,14,15,16";
            return ($this->db->insert("admin",$user)==1)&&($user['site_id']) ? (int)$this->db->insertId(): -1;
        }
    }
}